package com.example.gybicycle.controller.wx;

import com.example.gybicycle.common.entity.Entity;
import com.example.gybicycle.common.entity.NormalPage;
import com.example.gybicycle.common.entity.Result;
import com.example.gybicycle.common.enums.NumType;
import com.example.gybicycle.common.util.*;
import com.example.gybicycle.entity.*;
import com.example.gybicycle.entity.wx.OpenLock;
import com.example.gybicycle.entity.wx.WXLoginInfo;
import com.example.gybicycle.service.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;

@Tag(name = "管理员操作")
@RestController
@RequestMapping("/wx/admin")
public class AdminController {
    @Autowired
    private MemberOrderService orderService;
    @Autowired
    private MemberInsureService insureService;
    @Autowired
    private MemberDepositService depositService;
    @Autowired
    private MemberPayService payService;
    @Autowired
    private MemberCyclingService cyclingService;
    @Autowired
    private LockService lockService;
    @Autowired
    private BikeService bikeService;
    @Autowired
    private BikeFailureService failureService;
    @Autowired
    private MemberInsureService memberInsureService;
    @PostMapping("/openLock")
    @Operation(summary = "扫码开锁")
    public Result<MemberOrder> page(@RequestBody Bike bike, HttpServletRequest request){
        WXLoginInfo info= LoginUtil.getWxLoginInfo(request);
        if(info==null)
            return Result.fail("请获取用户信息");
        bike=bikeService.get(bike.getId());
        if(bike.getLockId()==null||bike.getLockId().length()==0)
            return Result.fail("当前车辆未关锁");
        Lock lock=lockService.get(bike.getLockId());
        BiCycleUtil.openLock(lock.getCid(),lock.getMainId(),info.getName());
        MemberOrder order=new MemberOrder();
        order.setBikeId(bike.getId());
        order.setStartTime(new Date());
        order.setOrderState(0);
        order.setStartPoint(lockService.findPointName(lock.getId()));
        order.setMemberId(info.getId());
        order.setPriceType(3);
        int num=orderService.insert(order);
        return Result.success(order);
    }
    @PostMapping("/closeLock")
    @Operation(summary = "扫码关锁")
    public Result  paypage(@RequestBody Bike entity, HttpServletRequest request){
        WXLoginInfo info= LoginUtil.getWxLoginInfo(request);
        if(info==null)
            return Result.fail("请获取用户信息");
        MemberOrder order=new MemberOrder();
        order.setBikeId(entity.getId());
        order.setOrderState(0);
        order=orderService.getOne(order);
        if(order==null)
            return Result.fail("未找到订单");
        order.setOrderState(1);
        order.setEndPoint("管理员扫码关锁");
        order.setEndTime(new Date());
        order.setDuration(DataUtil.Duration(order.getStartTime(),order.getEndTime()));
        if(order.getPriceType().equals(3))
            return Result.success(order);
        MemberPay pay=NumberUtil.createPay(order,info.getWxOpenid());
        order.setCyclingMoney(pay.getMoney());
        order.setInsureMoney(new BigDecimal(1));
        order.setPayMoney(pay.getMoney().add(new BigDecimal(1)));
        pay.setMoney(order.getCyclingMoney());
        int num=orderService.update(order);
        if(num==0)
            return Result.fail("关闭订单失败");
        num=payService.insert(pay);
        if(num>0)
            return Result.success(order);
        else return Result.fail("关锁失败");
    }
    @PostMapping("/failure")
    @Operation(summary = "故障记录")
    public Result<NormalPage<BikeFailure>> handpage(@RequestBody Entity entity, HttpServletRequest request){
        WXLoginInfo info= LoginUtil.getWxLoginInfo(request);
        if(info==null)
            return Result.fail("请获取用户信息");
        BikeFailure page=new BikeFailure();
        BeanUtils.copyProperties(entity,page);
//        page.setFailureState(0);
        Page<BikeFailure> list = PageHelper.startPage(page.getPage(), page.getSize()).doSelectPage(
                () ->failureService.findList(page));
        return Result.success(NormalPage.createPage(list));
    }
    @PostMapping("/failureDetail")
    @Operation(summary = "故障详情")
    public Result<BikeFailure> successpage(@RequestBody BikeFailure entity, HttpServletRequest request){
        WXLoginInfo info= LoginUtil.getWxLoginInfo(request);
        if(info==null)
            return Result.fail("请获取用户信息");
        entity=failureService.get(entity.getId());
        return Result.success(entity);
    }
    @PostMapping("/failureTackle")
    @Operation(summary = "故障处理")
    public Result failureTackle(@RequestBody BikeFailure entity, HttpServletRequest request){
        WXLoginInfo info= LoginUtil.getWxLoginInfo(request);
        if(info==null)
            return Result.fail("请获取用户信息");
        if(entity.getFailureState()==null||entity.getFailureState().equals(0))
            return Result.fail("请选择处理状态");
        if(entity.getId()==null)
            return Result.fail("未获取ID");
        entity.setConfirmTime(new Date());
        entity.setConfirmBy(info.getId());
        int num=failureService.update(entity);
        entity=failureService.get(entity.getId());
        if(entity.getFailureState().equals(1)){
            Bike bike=new Bike();
            bike.setBikeState(2);
            bike.setId(entity.getBikeId());
            bikeService.update(bike);
        }
        MemberOrder order=new MemberOrder();
        order=orderService.get(entity.getMemberOrderId());
        order.setEndTime(entity.getFailureTime());
        order.setOrderState(1);
        order.setDuration(DataUtil.Duration(order.getStartTime(),order.getEndTime()));

        MemberPay pay=NumberUtil.createPay(order,info.getWxOpenid());
        order.setCyclingMoney(pay.getMoney());
        order.setInsureMoney(new BigDecimal(0));
        order.setPayMoney(pay.getMoney());
        num=orderService.update(order);
//        order.setPayMoney(pay.getMoney().add(new BigDecimal(1)));
        pay.setMoney(order.getCyclingMoney());
//         创建支付单
        num=payService.insert(pay);

        return Result.success("故障处理成功");
    }
}
